package com.future.lock.home.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.MessageEvent;
import com.future.baselib.utils.EventBusUtil;
import com.future.baselib.view.CustomViewCenterDialog;
import com.future.lock.R;
import com.future.lock.common.Constants;
import com.future.lock.common.MyApp;
import com.future.lock.common.RefreshDataUtils;
import com.future.lock.common.http.HttpUtils;
import com.future.lock.common.http.response.DefaultHttpResponse;
import com.future.lock.home.adapter.LockNotesAdapter;
import com.future.lock.home.entity.bean.LockNotes;
import com.future.lock.home.entity.response.LockNotesResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LockNotesActivity extends BaseActivity {

    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.layout_title)
    FrameLayout layoutTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private CustomViewCenterDialog deleteDialog;
    private String lock_id;
    private List<LockNotes> lockNotes;
    private LockNotesAdapter adapter;


    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_lock_notes);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        setTitle("开锁记录");
        setTitleRight("清空", v -> {
            showDeleteDialog();
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new LockNotesAdapter(R.layout.item_lock_notes, R.layout.item_lock_notes_title, null);
        View emptyView = getLayoutInflater().inflate(R.layout.layout_default_empty, null);
        ((TextView) emptyView.findViewById(R.id.tv_tib)).setText("暂无开锁记录");
        adapter.setEmptyView(emptyView);
        recyclerView.setAdapter(adapter);

        getData();
    }

    private void getData() {
        HttpUtils.getHttpApi()
                .getLockNotesList(MyApp.getUserToken(), lock_id)
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        LockNotesResponse lockNotesResponse = new LockNotesResponse();
                        lockNotesResponse.parse(response.body());
                        if (lockNotesResponse.success == 1) {
                            lockNotes = lockNotesResponse.lockNotes;
                            updateNotes();
                            adapter.setNewData(lockNotes);
                            if (TextUtils.isEmpty(lock_id)) {
                                EventBusUtil.sendEvent(new MessageEvent(Constants.RED_POINT_NO));
                            }
                        } else {
                            if (lockNotesResponse.code == 112) {
                                RefreshDataUtils.logout(LockNotesActivity.this);
                            }
                            toast(lockNotesResponse.msg);
                        }
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        toast(t.getMessage());
                    }
                });
    }

    private void updateNotes() {
        String date = "";
        for (int i = 0; i < lockNotes.size(); i++) {
            LockNotes notes = lockNotes.get(i);
            if (!TextUtils.equals(date, notes.t.date)) {
                date = notes.t.date;
                lockNotes.add(i, new LockNotes(true, date));
                i++;
            }
        }
    }

    @Override
    protected void initLogic() {

    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        lock_id = extras.getString("lock_id", "");
    }

    private void showDeleteDialog() {
        if (deleteDialog == null) {
            View rootView = LayoutInflater.from(this).inflate(R.layout.dialog_default, null);
            ((TextView) rootView.findViewById(R.id.tv_message)).setText("是否清空开锁记录?");
            rootView.findViewById(R.id.btn_cancel).setOnClickListener(v -> {
                if (deleteDialog != null) {
                    deleteDialog.dismiss();
                }
            });
            Button btnOk = (Button) rootView.findViewById(R.id.btn_ok);
            btnOk.setText("确定");
            btnOk.setOnClickListener(v -> clearNotes());
            deleteDialog = new CustomViewCenterDialog(this)
                    .setView(rootView)
                    .builder();
        }
        deleteDialog.show();
    }

    private void clearNotes() {
        deleteDialog.dismiss();
        showLoadingDialog();
        HttpUtils.getHttpApi()
                .clearLockNotes(MyApp.getUserToken(), lock_id)
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        dissmissLoadingDialog();
                        DefaultHttpResponse httpResponse = new DefaultHttpResponse();
                        httpResponse.parse(response.body());
                        toast(httpResponse.msg);
                        if (httpResponse.success == 1) {
                            lockNotes = null;
                            adapter.setNewData(null);
                        }else{
                            if (httpResponse.code == 112) {
                                RefreshDataUtils.logout(LockNotesActivity.this);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        dissmissLoadingDialog();
                        toast(t.getMessage());
                    }
                });
    }
}
